home *** CD-ROM | disk | FTP | other *** search
-
- //
- // Copyright 1999 Macromedia, Inc. All rights reserved.
- //
- // ----------------------------------------------------
- //
- // Date.js
- //
- //This object inserts the current date and optionally time
- //and day at the current insertion point. An Update Automatically
- //On Save Option will update the date when the document is saved.
- //
- //
- //Localization Note:
- //Read the comments in the initGlobals function for instructions
- //on controlling the order of the date formats in the Date Format list,
- //as well as specifying that certain date formats should not appear
- //in a localized language.
-
-
-
- //*********************GLOBAL VARIABLES***********************
-
- //see initGlobals for info on global vars
-
- var helpDoc = MM.HELP_objDate;
-
- //global arrays
- var GarrDateFormats;
- var GarrDayFormats;
- var GarrTimeFormats;
-
- //form element objects
- var GlistDateFormats;
- var GselDayFormats;
- var GselTimeFormats;
- var GcbUpdate;
-
- //global strings needed for accessor functions
- var GstrFullDate;
- var GstrDateID;
-
- function initGlobals(){
-
- //This extension requires special localization work to adjust the order and/or conent
- //of the day,date, and time drop-down menus.
- //---------------------------------------------------------------------------------
- //
- //BEGIN LOCALIZATION WORK
- //
- //The GarrDateFormats array controls the order the date formats are shown in the UI
- //and the initially selected format.
- ///
- //1.Re-arrange the localized language to be the first item in the list.
- //(The first item is selected by default when the dialog loads.)
- //
- //2.Place any other formats that will be commonly used by that locale
- //as the second, third, fourth items, etc.
- //If the locale uses day-month-year instead of month-day-year,
- //move the month-day-year formats lower on the list
- //
- //3.Delete any formats that should not be shown. For instance,
- //the British1 format uses an abbreviated month. If this locale never
- //abbreviates its months (and therefore Arr_Abbr_Months cannot be translated),
- //delete the Engish1 format.
-
- GarrDateFormats = new Array("American1",
- "English2",
- "English1",
- "American3",
- "American2",
- "ISO8601",
- "Spanish1",
- "French1",
- "German1",
- "Brazilian1",
- "Italian1",
- "Swedish1",
- "Japanese1"
- );
-
- //Date Formating Examples:
- //American1 March 7, 1974
- //English1 7-mar-74
- //English2 07-Mar-1974
- //American2 3/7/74
- //American3 03/07/1974
- //ISO8601 1974-03-07
- //Spanish1 7/3/74 //also a common English format
- //French1 7/03/74
- //Brazilian1 07.03.74
- //German1 07.03.1974
- //Italian1 7-03-1974
- //Swedish1 7 march, 1974
- //Japanese1 74-03-07
-
- //--------------------------------
-
- ///The GarrDateFormats array controls the order the day formats are shown in the UI
- //and the initially selected format. Repeat the same instructions as above - that is,
- //re-arrange or delete the items according to the localized language
-
- //For instance, if a language does not have the concept of abbreviated days, then
- //delete or comment out all of the abbreviated formats. If a language does not ever
- //use the "," as a separator, then delete or comment out all of the comma formats.
-
- GarrDayFormats = new Array("NoDay",
- "FullDayComma",
- "FullDay",
- "AbbrDayComma",
- "AbbrDay",
- "LowAbbrDayComma",
- "LowAbbrDay"
- );
-
- //Day Formatting Examples:
- //NoDay [No Day]
- //FullDayComma Monday,
- //FullDay Monday
- //AbbrDayComma1 Mon,
- //AbbrDay1 Mon
- //AbbrDayComma2 mon,
- //AbbrDay2 mon
-
- //-------------------------------
-
- //The GarrTimeFormats array controls the order of the time formats.
- //For countries that use military time, consider switching the
- //second and third items.
-
-
- GarrTimeFormats = new Array("NoTime",
- "AMPMTime",
- "MilitaryTime"
- );
-
- //END LOCALIZATION WORK
-
- //-----------------------------------------------------------------------------
-
- //initialize global form elements
- theForm = document.forms[0];
- GlistDateFormats = theForm.DateFormats;
- GselDayFormats = theForm.DayFormats;
- GselTimeFormats = theForm.TimeFormats;
- GcbUpdate = theForm.Update;
- }
-
-
-
- //*********************** API ***********************
-
- //function: commandButtons
- //description: generic API function, returns string to be inserted at IP
-
- function commandButtons(){
- return new Array(BTN_OK, "setDateStr();window.close()",
- BTN_Cancel, "window.close()",
- BTN_Help, "displayHelp()" );
-
-
- }
-
- //***********************ACCESSOR FUNCTIONS***********************
-
- function getDateStr(){
- return GstrFullDate;
- }
-
- function getDateID(){
- return GstrDateID;
- }
-
- //***********************LOCAL FUNCTIONS***********************
-
- //function: setDateStr
- //description: called from OK button of dialog,
- //sets the global GstrFullDate variable
-
- function setDateStr(){
- var dateStr = "";
- var dateID = "";
- var now = new Date();
-
- var dayFormat = getSelectedOptionAttr(GselDayFormats,"value");
- var dateFormat = getSelectedOptionAttr(GlistDateFormats,"value");
- var timeFormat = getSelectedOptionAttr(GselTimeFormats,"value");
-
- //create the date that is inserted
- dateStr += createDayStr(now,dayFormat);
- dateStr += createDateStr(now,dateFormat);
- dateStr += createTimeStr(now,timeFormat);
-
- //dateID is inserted into the format attribute of the lock
- //and also placed in the opening comment
- dateID = createDateID(dayFormat,dateFormat,timeFormat);
-
- //if Update Automatically On Save is visible and checked,
- //add locks around the date
- if (GcbUpdate!=null && GcbUpdate.checked){
- dateStr = addLockMarkup(dateStr,dateID);
- }
-
- //assign to global variables accessed by accessor functions
- GstrFullDate = dateStr;
- GstrDateID = dateID;
- }
-
-
-
- //function: addLockMarkup
- //description: adds correct lock markup to the date that is
- //inserted
-
- function addLockMarkup(dateStr,dateID){
-
- var openBracket = "%3C";
- var closeBracket = "%3E";
- var quote = "%22";
-
- var openComment = openBracket + '!-- #BeginDate ' +
- 'format:' + dateID + ' --' + closeBracket;
- var closeComment = openBracket + '!-- #EndDate --' + closeBracket;
- var origAttr = openComment + dateStr + closeComment;
-
- var openLock = '<MM:BeginLock type="mmdate" format="' + dateID +
- '" orig="' + origAttr + '">';
- var closeLock = '<MM:EndLock>';
-
- return openLock + dateStr + closeLock;
- }
-
-
-
- //function: initializeUI
- //description: initializes the global variables, and populates
- //the UI with date format examples.
-
- function initializeUI(){
- //return if already initialized
- //(this happens if command is called from PI)
- if (GarrDateFormats){
- GlistDateFormats.focus();
- return;
- }
-
- initGlobals(); //initialize global variables
- populateUI(); //populate UI
- }
-
-
-
- //function: populateUI
- //description: populate the UI with date format examples
-
- function populateUI(){
- var dateFormatsArr = GarrDateFormats; //shorter names easier to work with
- var dayFormatsArr = GarrDayFormats;
- var timeFormatsArr = GarrTimeFormats;
-
- var nDateFormats = dateFormatsArr.length;
- var nDayFormats = dayFormatsArr.length;
- var nTimeFormats = timeFormatsArr.length;
-
- var dateObj = new Date("74","2","7","22","18"); //examples are for March 7, 1974
- var dateStr = "",timeStr="",dayStr="";
-
- //populate day format list in UI
- //the first line creates an array of formatted dayes
- //(the createDayStr function is overloaded to return one item
- //or an array, therefore, unfortunately the function name isn't
- //always entirely accurate)
- dayFormatsArr = createDayStr(dateObj,dayFormatsArr,true);
- var counter = 0;
- for (i in dayFormatsArr){
- if (!dayFormatsArr[i].prototype){
- // Val 13-aug-99 use temporary for speedier UI loading
- var curr = new Option(dayFormatsArr[i]);
- curr.value = i;
- GselDayFormats.options[ counter++ ] = curr;
- }
- }
-
- //populate date format list in UI
- //the first line creates an array of formatted dates
- //(the createDateStr function is overloaded to return one item
- //or an array, therefore, unfortunately, the function name isn't
- ///always entirely accurate)
- dateFormatsArr = createDateStr(dateObj,dateFormatsArr);
- counter = 0;
- for (i in dateFormatsArr){
- if (!dateFormatsArr[i].prototype){
- // Val 13-aug-99: use a temporary to make initialization faster,
- // and only assign to the formats array once.
- var currObj = new Option(dateFormatsArr[i]);
- currObj.value = i;
- GlistDateFormats.options[ counter++ ] = currObj;
- }
- }
-
- //populate time format list
- for (i=0;i<nTimeFormats;i++){
- timeStr = createTimeStr(dateObj, timeFormatsArr[i], true);
- GselTimeFormats.options[ i ] = new Option(timeStr);
- GselTimeFormats.options[ i ].value = timeFormatsArr[i];
- }
-
-
- //select first option of each menu
- GselDayFormats.selectedIndex = 0;
- GlistDateFormats.selectedIndex = 0;
- GselTimeFormats.selectedIndex = 0;
-
- //put focus in date formats field
- GlistDateFormats.focus();
- }
-
-
-
- //function: lead
- //description: given a one or two digit number,
- //adds a leading 0 if a 1 digit number
-
- function lead(num){
- if (num.toString().length == "1")
- return "0" + num;
- return ( num );
- }
-
-
- //function: createDateStr
- //description: given a date obj and a date format or formats
- //returns an array with the correctly formatted date strings
- //overloaded: dateFormat can be one item or an array
- //if it is one item, returns one item
- //if it is an array, returns an array
-
- function createDateStr(dateObj,dateFormat){
- var date = dateObj.getDate();
- var day = dateObj.getDay();
- var month = dateObj.getMonth();
- var abbrMonth = ARR_AbbrMonths[ month ];
- var fullMonth = ARR_FullMonths[ month++ ];
- var year = dateObj.getYear();
- var abbrYear = (year<100)? year : year.toString().substring(1);
- var fullYear = (year<100)? "19" + year : year + 1900;
-
- var retVal; //return value;
-
- //the dateFormat argument is overloaded so that it
- //can be either a string or an array. Handle accordingly.
- if (typeof dateFormat == "string"){
- retVal = createCorrectDateFormat(dateFormat,date,day,month,abbrMonth,fullMonth,
- year,abbrYear,fullYear);
- } else { //dateFormat is an array
- retVal = new Array();
- dateFormats = dateFormat; //rename
- var nFormats = dateFormats.length;
-
-
- for (var i=0;i<nFormats;i++){
- retVal[dateFormats[i]] = createCorrectDateFormat(dateFormat[i],date,day,month,abbrMonth,
- fullMonth,year,abbrYear,fullYear);
-
- }
- }
-
- return retVal;
- }
-
-
- //function: createCorrectDateFormat
- //description: returns a correclty formatted date string
-
- function createCorrectDateFormat(dateFormat,date,day,month,abbrMonth,fullMonth,
- year,abbrYear,fullYear,time){
-
- var dateStr = "";
-
- switch (dateFormat){
-
- case "American1": // Thursday, March 7, 1974
- dateStr += fullMonth + " " + date + ", " + fullYear;
- break;
-
- case "American2": // 3/7/74
- dateStr += month + "/" + date + "/" + abbrYear;
- break;
-
- case "American3": // 03/07/1974
- dateStr += lead(month) + "/" + lead(date) + "/" + fullYear;
- break;
-
- case "ISO8601": // 1974-03-07
- dateStr += fullYear + "-" + lead(month) + "-" + lead(date);
- break;
-
- case "English1": // 7-mar-74
- dateStr += date + "-" + abbrMonth + "-" + abbrYear;
- break;
-
- case "English2": // 07-Mar-1974
- abbrMonth = abbrMonth.charAt(0).toUpperCase()+abbrMonth.substring(1);
- dateStr += lead(date) + "-" + abbrMonth + "-" + fullYear;
- break;
-
- case "Spanish1": // 7/3/74
- dateStr += date + "/" + month + "/" + abbrYear;
- break;
-
- case "French1": // 7/03/74
- dateStr += date + "/" + lead(month) + "/" + abbrYear;
- break;
-
- case "Italian1": // 7-03-1974
- dateStr += date + "-" + lead(month) + "-" + fullYear;
- break;
-
- case "Brazilian1": // 07.03.74
- dateStr += lead(date) + "." + lead(month) + "." + abbrYear;
- break;
-
- case "German1": // 07.03.1974
- dateStr += lead(date) + "." + lead(month) + "." + fullYear;
- break;
-
- case "Japanese1": // 74-03-07
- dateStr += abbrYear + "-" + lead(month) + "-" + lead(date);
- break;
-
- case "Swedish1": // 7 March, 1974
- dateStr += date + " " + fullMonth + ", " + fullYear;
- break;
-
- default:
- break;
- }
-
- return dateStr;
-
- }
-
-
-
- //function: createDayStr
- //description: see createDateStr notes. Except of course this function
- //returns a correctly formatted day (or days) instead of a date
-
- function createDayStr(dateObj,dayFormat,bPreview){
- day = dateObj.getDay();
- fullDay = ARR_FullDays[day];
- abbrDay = ARR_AbbrDays[day];
-
- if (typeof dayFormat == "string"){
- retVal = createCorrectDayFormat(dayFormat,fullDay,abbrDay,bPreview);
- } else { //dayFormat is an array
- retVal = new Array();
- dayFormats = dayFormat; //rename for clarity
- var nFormats = dayFormats.length;
-
- for (var i=0;i<nFormats;i++){
- retVal[dayFormats[i]] = createCorrectDayFormat(dayFormat[i],fullDay,abbrDay,bPreview);
- }
- }
- return retVal;
- }
-
-
-
- //function: createCorrectDayFormat
- //description: returns the correctly formatted day format
-
- function createCorrectDayFormat(dayFormat,fullDay,abbrDay,bPreview){
-
- var dayStr = "";
-
- switch (dayFormat){
-
- case "NoDay":
- if (bPreview)
- dayStr = "[" + OPTION_NoDay + "]";
- break;
-
- case "FullDayComma":
- dayStr = fullDay + ", ";
- break;
-
- case "FullDay":
- dayStr = fullDay + " ";
- break;
-
- case "AbbrDayComma":
- dayStr = abbrDay + ", ";
- break;
-
- case "AbbrDay":
- dayStr = abbrDay + " ";
- break;
-
- case "LowAbbrDayComma":
- dayStr = abbrDay.toLowerCase() + ", ";
- break;
-
- case "LowAbbrDay":
- dayStr = abbrDay.toLowerCase() + " ";
- break;
-
- default:
- break;
-
- }
-
- return dayStr;
-
- }
-
-
-
- //function: createTimeStr
- //description: given a dateObj and a time format,
- //returns the correctly formatted time string
- //The time format argument is "a" for AM/PM,
- //"m" for military time, and "" for no time
-
- function createTimeStr(dateObj,timeFormat,bPreview){
- var hours = dateObj.getHours();
- var minutes = lead(dateObj.getMinutes());
- var timeStr = ""; //return value
-
- switch (timeFormat){
- case "NoTime":
- if (bPreview)
- timeStr = "[" + OPTION_NoTime + "]";
- break;
-
- case "AMPMTime":
- timeStr += (hours>12) ? hours-12 + ":" + minutes + " " + PM :
- hours + ":" + minutes + " " + AM;
- timeStr = " " + timeStr;
- break;
-
- case "MilitaryTime":
- timeStr += " " + hours + ":" + minutes;
- timeStr = " " + timeStr;
- break;
-
- default:
- break;
- }
-
- return timeStr;
- }
-
-
-
-
-